﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using Common.Entity;
using Newtonsoft.Json;

namespace Common
{
    public class CheckUpdate
    {
        /// <summary>
        /// 检查更新软件
        /// </summary>
        /// <returns></returns>
        public async Task<UpData> CheckVersion()
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    HttpResponseMessage response = await client.GetAsync(Config.UpdateUrl);
                    if (response.IsSuccessStatusCode)
                    {
                        string content = await response.Content.ReadAsStringAsync();
                        UpReleasesEntity releasesEntity = JsonConvert.DeserializeObject<UpReleasesEntity>(content);
                        UpData upData = null;
                        //判断版本号是否相同
                        if (releasesEntity.tag_name != Config.Version)
                        {
                            upData=new UpData();
                            upData.Version = releasesEntity.tag_name;
                            upData.name = releasesEntity.name;
                            upData.downName = releasesEntity.assets[0].name;
                            upData.Updownurl = releasesEntity.assets[0].browser_download_url;
                        }
                        return upData;
                    }
                    else
                    {
                        throw new Exception($"更新请求失败！");
                    }
                }
                //return true;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

    }
}
